package com.his.diagnose.service.Imp;

import com.his.diagnose.dao.DictMedicalcodeIcdMapper;
import com.his.diagnose.entity.DictMedicalcodeIcd;
import com.his.diagnose.repository.DiagnoseRepository;
import com.his.diagnose.service.DictMedicalCodeIcdService;
import com.his.diagnose.vo.SearchDiagnosisVo;
import com.his.dictitem.entity.DictItem;
import com.his.login.entity.DictEmployee;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.DisMaxQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author xiaoxiaoping
 * @Date 2020/3/24 18:37
 * @Version 1.0
 */
@Service
public class DictMedicalCodeIcdServiceImpl implements DictMedicalCodeIcdService {
    @Resource
    private DictMedicalcodeIcdMapper dictMedicalcodeIcdMapper;
    @Resource
    private DiagnoseRepository diagnoseRepository;
    @Override
    public void saveToEs() {
        List<DictMedicalcodeIcd> list = dictMedicalcodeIcdMapper.select();
        diagnoseRepository.saveAll(list);
    }

    @Override
    public List<DictMedicalcodeIcd> queryIcdByCondition(DictEmployee dictEmployee, SearchDiagnosisVo searchDiagnosisVo) {
        DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        if(!StringUtils.isEmpty(searchDiagnosisVo.getSearchWord())) {
            QueryBuilder ikNameQuery = QueryBuilders.matchQuery("icdname", searchDiagnosisVo.getSearchWord());
            QueryBuilder pinyinNameQuery = QueryBuilders.matchQuery("icdname.pinyin", searchDiagnosisVo.getSearchWord());
            disMaxQueryBuilder.add(pinyinNameQuery);
            disMaxQueryBuilder.add(pinyinNameQuery);
            builder.should(disMaxQueryBuilder);
        }
        QueryBuilder hospIdQuery = QueryBuilders.termQuery("hospid",dictEmployee.getHOSPID());
        if(!StringUtils.isEmpty(searchDiagnosisVo.getYbTypeId())){
            QueryBuilder ybTypeIdQuery = QueryBuilders.termQuery("ybtypeid",searchDiagnosisVo.getYbTypeId());
            builder.must(ybTypeIdQuery);
        }
        if(!StringUtils.isEmpty(searchDiagnosisVo.getIcdType())){
            QueryBuilder icdTypeQuery  = QueryBuilders.termQuery("icdtype",searchDiagnosisVo.getIcdType());
            builder.must(icdTypeQuery);
        }

        //disMaxQueryBuilder.add(ikNameQuery);

        builder.must(hospIdQuery);
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(builder)
                .build();
        System.out.println(searchQuery.getQuery().toString());
        List<DictMedicalcodeIcd> list  = diagnoseRepository.search(searchQuery).getContent();
        return list;
    }
}
